﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstrumentCheck.Module;
using InstrumentCheck.BLL;
using System.Collections.Generic;


namespace InstrumentCheck.WebAppUI
{
    
    public partial class ReceiveBox : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                User user = Session["UserInfo"] as User;
                OnPageLoad(user);

            }

        }
        protected void OnPageLoad(User user)
        {
            string UserNO = user.EmpNO.ToString();
            //int Staus = int.Parse(ddlResult.SelectedValue.ToString());
            int Status = Convert.ToInt16(ApproveStatus.Approving);
            List<ApplyForm> applyform = new List<ApplyForm>();
            applyform = ReceiveBox_bll.ReceiveData(UserNO, Status);
            ViewState["list"] = applyform;
            bindGridView();
        }
        protected void bindGridView()
        {
            List<ApplyForm> applyform = ViewState["list"] as List<ApplyForm>;
            gv.DataSource = applyform;
            gv.DataBind();
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            { 
                ApplyForm form = e.Row.DataItem as ApplyForm;
                e.Row.Cells[1].Text = getUrl(form.FormID,form.FormNO);
                e.Row.Cells[3].Text = form.Applicant.EmpChineseName;
                e.Row.Cells[4].Text = form.Item.AssetNO.ToString();
                e.Row.Cells[5].Text = form.Item.ChiName.ToString();
                e.Row.Cells[6].Text = form.Item.Spec.ToString();
                e.Row.Cells[7].Text = form.Item.ModelNO.ToString();
                e.Row.Cells[8].Text = form.Item.NeedCorrectDate.ToString();
                FormRouting  formrouting = new FormRouting();
                formrouting = form.FormRoutingHistory[0];
                e.Row.Cells[9].Text = getApprovalStatus(formrouting);

            }
        }
        protected string getUrl(int FormID,int FormNO)
        {
            string formno = FormNO.ToString("0000000000");
            string URL = "<a href=" + PageURL() + "\\CorrectItem.aspx?ID=" +FormID + ">" + formno + "</a>";
            return URL;
        }
        protected string getApprovalStatus(FormRouting routing)
        {
            string ApprovalStatus = "";
            switch (routing.ApproveStatus)
            { 
                case ApproveStatus.Approving:
                    ApprovalStatus = "簽核中";
                    break;
                case ApproveStatus.Approved:
                    ApprovalStatus = "已簽核";
                    break;
                default :
                    break;
            }
            return ApprovalStatus;
        }

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (gv.PageIndex > gv.PageCount)
                gv.PageIndex = gv.PageIndex;
            else
                gv.PageIndex = e.NewPageIndex;
            bindGridView();
        }

        protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                e.Row.Cells[0].Visible = false;
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Cells[0].Visible = false;
        }
       

    }
}
